package cn.prophet.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.prophet.bean.GameOrder;
import cn.prophet.bean.vo.SuperResponseBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.prophet.bean.BjksRecord;
import cn.prophet.bean.vo.SuperResponseListBean;
import cn.prophet.service.BjKRecordService;
import lombok.extern.slf4j.Slf4j;


@Controller
@RequestMapping("bjk")
@Slf4j
public class BjksController {
    @Autowired
    private BjKRecordService recordService;

    @GetMapping("start")
    @ResponseBody
    public SuperResponseListBean<BjksRecord> gainPersonInfo(@RequestParam(value = "gameCategory",required = true)Byte gameCategory) {
        
        SuperResponseListBean<BjksRecord> bean = new SuperResponseListBean<>();
        List<BjksRecord> records = recordService.getBjksRecord(gameCategory);
        if(records!=null&&records.size()>0) {
            bean.setCode(1);

            bean.setMsg("查询成功");
        }else {
            bean.setCode(0);
            bean.setMsg("暂无数据");
        }
        bean.setServerTime(new Date());
        bean.setData(records);
        return bean;
        
    }
    @GetMapping("bjksintropage")
    public String getBjIntroPage(){
        return "bjks_intro";
    }
    @RequestMapping("betting")
    @ResponseBody
    public SuperResponseBean<String> bettingGame(@RequestParam(value = "gameCategory",required = true) Byte gameCategory, @RequestParam(value = "userId",required = true)Long userId, @RequestParam(value = "gameType",required = true) Byte gameType, @RequestParam(value = "gameResult",required = true) String gameResult,@RequestParam(value = "recordId",required = true) String recordId, @RequestParam(value = "buyNum",required = true) Byte buyNum, @RequestParam(value = "multiple",required = true) Short multiple, @RequestParam(value = "singleAmount",required = true) BigDecimal singleAmount,@RequestParam(value = "bettingAmount",required = true) BigDecimal bettingAmount,@RequestParam(value = "winningAmount",required = true) BigDecimal winningAmount){
        int result = recordService.bettingGame(gameCategory,userId,gameType,gameResult,recordId,buyNum,multiple,singleAmount,bettingAmount,winningAmount);
        SuperResponseBean<String> bean = new SuperResponseBean<>();
        if(result==1){
            bean.setCode(1);
            bean.setMsg("提交订单成功");
        }else if(result==2){
            bean.setCode(0);
            bean.setMsg("本期号码不在押注时间内");
        }else if(result==3){
            bean.setCode(0);
            bean.setMsg("押注结果集不能为null");
        }else if(result==4){
            bean.setCode(0);
            bean.setMsg("余额不足，请先充值");
        }else if(result==5){
            bean.setCode(0);
            bean.setMsg("用户不存在！");
        }else{
            bean.setCode(0);
            bean.setMsg("提交订单失败");
        }
        return bean;
    }
    @RequestMapping("order")
    @ResponseBody
    public SuperResponseListBean<GameOrder> getGameOrder(@RequestParam(value = "userId",required = true) Long userId,Byte showType,@RequestParam(value = "gameCategory",required = true) Byte gameCategory){
        List<GameOrder> list = recordService.getGameOrder(userId,showType,gameCategory);
        SuperResponseListBean<GameOrder> bean = new SuperResponseListBean<>();
        if(list!=null&&list.size()>0){
            bean.setCode(1);
            bean.setMsg("查询成功");
        }else{
            bean.setCode(0);
            bean.setMsg("暂无订单");
        }
        bean.setData(list);
        return bean;
    }
    @GetMapping("customer")
    public String customerCenter(){
        return "customer_center";
    }
    @GetMapping("unreadMsg")
    @ResponseBody
    public Long getUnReadMsg(@RequestParam(value = "userId",required = true) Long userId){

        return recordService.getUnraadNum(userId);
    }
    @GetMapping("mailbox")
    @ResponseBody
    public SuperResponseListBean<GameOrder> getMailBox(@RequestParam(value = "userId",required = true) Long userId){
        SuperResponseListBean listBean = new SuperResponseListBean();
        List<GameOrder> list = recordService.getMailBox(userId);
        if(list!=null&&list.size()>0){
            listBean.setCode(1);
            listBean.setMsg("查询成功");
        }else{
            listBean.setCode(0);
            listBean.setMsg("查询失败");
        }
        listBean.setData(list);
        return listBean;
    }

    @GetMapping("chaseNumber")
    @ResponseBody
    public Map<String,String> chaseNumber(@RequestParam(value = "userId",required = true) Long userId, @RequestParam(value = "gameId",required = true) Long gameId, Integer chaseCount, Short multiple, Integer period, Integer beiNum, BigDecimal money, Byte isStop, Byte gameCategory, Byte gameType, String gameResult, Byte buy_num, BigDecimal single_amount, BigDecimal winningAmount){

        Map<String,String> map = new HashMap<>();
        int result = recordService.chaseNumber(userId,gameId,chaseCount,multiple,period,beiNum,money,isStop,gameCategory,gameType,gameResult,buy_num,single_amount,winningAmount);
        if(result==1){
            map.put("code","1");
            map.put("msg","追号成功");
        }else if(result==3){
            map.put("code","0");
            map.put("msg","余额不足");
        }else if(result==0){
            map.put("code","0");
            map.put("msg","请校验最新期号");
        }else{
            map.put("code","0");
            map.put("msg","此号码正在追号，请从"+(result+1)+"期开始追！");
        }
        return map;
    }
    @GetMapping("getRecordByDate")
    @ResponseBody
    public SuperResponseListBean<BjksRecord> getRecordByDate(Byte gameGategory,String date){
        List<BjksRecord> list = recordService.getRecordByDate(gameGategory,date);
        SuperResponseListBean<BjksRecord> listBean = new SuperResponseListBean<>();
        if(list!=null&&list.size()>0){
            listBean.setCode(1);
            listBean.setMsg("查询成功");
        }else{
            listBean.setCode(0);
            listBean.setMsg("查询失败");
        }

        listBean.setData(list);

        return listBean;
    }

}
